package io.renren.modules.student.service.impl;

import cn.hutool.core.util.PageUtil;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.SnowFlake;
import io.renren.modules.student.dao.StuExploreDao;
import io.renren.modules.student.service.StuExploreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class StuExploreServiceImpl implements StuExploreService {
    @Autowired
    private StuExploreDao stuExploreDao;
    @Override
    public PageUtils queryList(Map<String, Object> param) {
        List<Map<String, Object>> stu = stuExploreDao.queryList(param);
        int total = stuExploreDao.queryTotal(param);
        int limit = Integer.parseInt(param.get("limit").toString());
        int page = Integer.parseInt(param.get("page").toString());
        PageUtils pageUtil = new PageUtils(stu,total,limit,page);
        return  pageUtil;
    }
  /*
  查询最新课程数据
   */
    @Override
    public Map<String, Object> queryNewData() {
        Map<String, Object> result = stuExploreDao.queryNewData();
        return result;
    }
  /*
  获得每个课程的基本信息
   */
    @Override
    public Map<String, String> queryTeacherInfo(Map<String, Object> param) {
        Map<String, String> result = stuExploreDao.queryTeacherInfo(param);
        return result;
    }

    @Override
    public List<Map<String, String>> queryChapter(Map<String, Object> param) {
       List<Map<String, String>>  result = stuExploreDao.queryChapter(param);
        return result;
    }
    /*
    获得每个课程介绍
     */
    @Override
    public Map<String, String> queryCourseIntro(Map<String, Object> param) {
        String studentId =stuExploreDao.queryStudent(param);
        param.put("studentId",studentId);
        System.out.println("studentId"+studentId);
        Map<String, String> result = stuExploreDao.queryCourseIntro(param);
       if(stuExploreDao.queryIfAddCourse(param) > 0){//判断是否为空
           result.put("ifAddCourse","1");//如果为1时则选了该课
       }else{
           result.put("ifAddCourse","0");//如果为0时则没选该课
       }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCourse(Map<String, Object> param) {
        SnowFlake snowFlake = new SnowFlake(3L,10L);
        param.put("studentCourseId",snowFlake.nextId()+"");
        String studentId =stuExploreDao.queryStudent(param);
        param.put("studentId",studentId);
        if(studentId==null||studentId.trim()==""){
            throw new RRException("未查询到相关学生");
        }
        param.put("date",new Date());
        stuExploreDao.insertCourse(param);
    }

    @Override
    public List<Map<String, Object>> queryContent(String chapterId) {
        List<Map<String,Object>> result = stuExploreDao.queryContent(chapterId);
        return result;
    }

    @Override
    public Map<String, String> queryChapterData(Map<String, Object> param) {
        Map<String, String>  result =  stuExploreDao.queryChapterData(param);
        return result;
    }


}
